/**
 * Created by xingliu on 2018/5/22.
 */

// 设置cookie
export const setCookie = (c_name,value,expiremMinutes) => {
    var exdate = new Date();
    exdate.setTime(exdate.getTime() + expiremMinutes * 60 * 1000);
    document.cookie= c_name + "=" + value+((expiremMinutes==null) ? "" : ";expires="+exdate.toGMTString());
}

// 读取cookie
export const getCookie =(c_name) => {
   /* if (document.cookie.length>0)
    {
        var c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            var c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1)
                c_end = document.cookie.length
            console.log(JSON.stringify(document.cookie.substring(c_start, c_end)))
            return document.cookie.substring(c_start, c_end)
        }
    }
    return ""*/
   /* var arr, reg = new RegExp("(^| )" + c_name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return (arr[2]);
    else
        return null;*/
    var arrStr = document.cookie.split("; ");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] == c_name)
            return unescape(temp[1]);
    }
}

// 删除cookie
export const delCookie =(c_name) => {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(c_name);
    if(cval!=null){
        document.cookie = c_name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}